﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IPCPhotoEditor.ChildForms;
using LogLib;
using KAImage.Config;

namespace IPCPhotoEditor
{
    public partial class FormApplyConfig : Form
    {
        public FormApplyConfig()
        {
            ReLoadConfig();
            InitTxtFiles();
            InitializeComponent();
        }

        private List<string> listHelpTxts = new List<string>();

        private AlgorithmApplyingOption ApplyingOption = KAConfigs.AlgorApplying;

        private void ReLoadConfig()
        {
            try
            {
                ConfigService service = ConfigService.GetInstance();
                service.LoadConfig();
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
            }
        }

        private void InitTxtFiles()
        {
            listHelpTxts.Add(string.Format("{0}//help//色相饱和度介绍.txt", Application.StartupPath));
            listHelpTxts.Add(string.Format("{0}//help//色阶调整.txt", Application.StartupPath));
            listHelpTxts.Add(string.Format("{0}//help//曲线介绍.txt", Application.StartupPath));
            listHelpTxts.Add(string.Format("{0}//help//模糊和锐化.txt", Application.StartupPath));
            listHelpTxts.Add(string.Format("{0}//help//图层混合原理.txt", Application.StartupPath));
        }

        private void InitCtrls()
        {
            metroToggleBlurSharpen.Checked = ApplyingOption.BlurSharpen;
            metroToggleCurve.Checked = ApplyingOption.Curve;
            metroToggleHSL.Checked = ApplyingOption.HSL;
            metroToggleLevel.Checked = ApplyingOption.Levels;
            metroToggleMerge.Checked = ApplyingOption.Megre;
        }

        private void UpdateConfig()
        {
            ApplyingOption.BlurSharpen = metroToggleBlurSharpen.Checked;
            ApplyingOption.Curve = metroToggleCurve.Checked;
            ApplyingOption.HSL = metroToggleHSL.Checked;
            ApplyingOption.Levels = metroToggleLevel.Checked;
            ApplyingOption.Megre = metroToggleMerge.Checked;
            ApplyingOption.Save();
        }

        private void FormApplyConfig_Load(object sender, EventArgs e)
        {
            InitCtrls();
        }

        private void ShowHelpInfo(string txtFile)
        {
            FormIntroduction form = new FormIntroduction(txtFile);
            form.ShowDialog();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            UpdateConfig();
            this.Close();
        }

        private void buttonHelpHSL_Click(object sender, EventArgs e)
        {
            ShowHelpInfo(listHelpTxts[0]);
        }

        private void buttonHelpLevel_Click(object sender, EventArgs e)
        {
            ShowHelpInfo(listHelpTxts[1]);
        }

        private void buttonHelpCurve_Click(object sender, EventArgs e)
        {
            ShowHelpInfo(listHelpTxts[2]);
        }

        private void buttonHelpBlurSharpen_Click(object sender, EventArgs e)
        {
            ShowHelpInfo(listHelpTxts[3]);
        }

        private void buttonHelpMegre_Click(object sender, EventArgs e)
        {
            ShowHelpInfo(listHelpTxts[4]);
        }

        private void buttonHSL_Click(object sender, EventArgs e)
        {
            FormHueSat form = new FormHueSat();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
            }           
        }

        private void buttonCurve_Click(object sender, EventArgs e)
        {
            FormCurves form = new FormCurves();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
            }
        }

        private void buttonBlurSharpen_Click(object sender, EventArgs e)
        {
            FormImageEffects form = new FormImageEffects();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
            }
        }

        private void buttonMegre_Click(object sender, EventArgs e)
        {
            FormMerging form = new FormMerging();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
            }
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            UpdateConfig();
            ReLoadConfig();
        }

        private void buttonLevel_Click(object sender, EventArgs e)
        {
            FormLevel form = new FormLevel();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
